﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartMathLibrary.Space.CoreTypes;

namespace SmartMathLibrary.Space.QueryEngine
{
    /// <summary>
    /// This class represents a substring query.
    /// </summary>
    [Serializable]
    public class SubStringQuery : AbstractSpaceQuery
    {
        /// <summary>
        /// The compare value.
        /// </summary>
        private String value;

        /// <summary>
        /// The string comparison option.
        /// </summary>
        private StringComparison comparison;

        /// <summary>
        /// Initializes a new instance of the <see cref="SubStringQuery"/> class.
        /// </summary>
        /// <param name="value">The compare value.</param>
        public SubStringQuery(String value)
            : base()
        {
            this.value = value;
            this.comparison = StringComparison.InvariantCultureIgnoreCase;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SubStringQuery"/> class.
        /// </summary>
        /// <param name="value">The compare value.</param>
        /// <param name="comparison">The string comparison option.</param>
        public SubStringQuery(String value, StringComparison comparison)
            : base()
        {
            this.value = value;
            this.comparison = comparison;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SubStringQuery"/> class.
        /// </summary>
        /// <param name="value">The compare value.</param>
        /// <param name="comparison">The string comparison option.</param>
        /// <param name="status">The object status criteria.</param>
        public SubStringQuery(String value, StringComparison comparison, SpaceObjectStatus status)
            : base(status)
        {
            this.value = value;
            this.comparison = comparison;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SubStringQuery"/> class.
        /// </summary>
        /// <param name="value">The compare value.</param>
        /// <param name="comparison">The string comparison option.</param>
        /// <param name="limit">The maximum number of query results.</param>
        public SubStringQuery(String value, StringComparison comparison, int limit)
            : base(limit)
        {
            this.value = value;
            this.comparison = comparison;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SubStringQuery"/> class.
        /// </summary>
        /// <param name="value">The compare value.</param>
        /// <param name="comparison">The string comparison option.</param>
        /// <param name="limit">The maximum number of query results.</param>
        /// <param name="status">The object status criteria.</param>
        public SubStringQuery(String value, StringComparison comparison, int limit, SpaceObjectStatus status)
            : base(limit, status)
        {
            this.value = value;
            this.comparison = comparison;
        }

        /// <summary>
        /// Gets or sets the compare value.
        /// </summary>
        /// <value>The compare value</value>
        public String Value
        {
            get { return this.value; }
            set { this.value = value; }
        }

        /// <summary>
        /// Gets or sets the string comparison option.
        /// </summary>
        /// <value>The string comparison option.</value>
        public StringComparison Comparison
        {
            get { return this.comparison; }
            set { this.comparison = value; }
        }

        /// <summary>
        /// Defines the query criteria of the specified query.
        /// </summary>
        /// <param name="item">The query source items.</param>
        /// <returns>
        /// The space ID fulfills the query criteria the method returns true otherwise, false.
        /// </returns>
        public override bool QueryDefinition(ISpaceEntry item)
        {
            if (item is SpaceString)
            {
                return !(((SpaceString) item).Data.IndexOf(this.value, this.comparison) < 0);
            }

            return false;
        }
    }
}